package proxy.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKMeipo implements InvocationHandler {
    private Object target;

    public Object getInstance(Object target) {
        try {
            this.target = target;
            Class<?> clazz = target.getClass();
            return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);

        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object obj = method.invoke(this.target,args);
        after();
        return obj;
    }

    private void before() {
        System.out.println("I am Meipo: Requirement accepted and try to find your a girlfriend now");
        System.out.println("Start matching......");
    }

    private void after() {
        System.out.println("Match found, if fit then get marry");
    }



}
